avatar

D W

Brick walls are there for a reason, they let us prove how badly we want things.

>

Posts List

Mount Windows Partitions on Ubuntu April 12, 2016
最短路径问题 October 10, 2015
Story Continued – 保研之路 September 28, 2015
Reading Computer Systems(A Programmer’s Perspective):2 August 27, 2015
乘法逆元 Euclid定理和中国剩余定理 August 22, 2015
Reading Computer Systems(A Programmer’s Perspective):1 August 14, 2015
Half Way Conclusion of 3rd Grade in College April 23, 2015
git远程代码管理,SSH还是HTTPS April 5, 2015
Moving My Blog to Octopress April 5, 2015
Monster Storm March 25, 2015
Review VCool Website March 23, 2015
Morris Traversal March 22, 2015
豆瓣笔试 March 20, 2015
LeetCode上面的Distinct Subsequences总结 December 20, 2014
LeetCode上面的WordLadder总结 November 25, 2014
Linux文件系统基础 November 14, 2014
第一篇博客 October 15, 2014
Markdown Style Guide March 3, 2014

git远程代码管理,SSH还是HTTPS

| Comments

之前学习git的过程中就遇到了这个问题,电脑并不能使用ssh连接远程仓库,例如clone git@github.com/xx.git 这样的工程会失败。我就必须改成使用HTTPS协议,把连接改成 https 的,或者直接配置一个环境变量

git config --global url."https://".insteadOf git://

这样处理有一个弊端,我在Ubuntu上面任何时候需要连接远程仓库,必须输入我的用户名和密码,这样有点麻烦,而SSH协议是用本地存储的公钥私钥验证的,只要配置好了就不用输入用户名密码,我决定改成SSH连接。

原因

电脑无法用SSH连接远程仓库的原因是SSH协议用的是22端口,但是有些防火墙会把这个端口block掉

解决方案

既然不让用22号端口,换个端口就可以了,就像上面stack overflow的解决方案一样,改用443端口,在 ~/.ssh/ 建立一个config文件,写上

Host github.com
User git
Hostname ssh.github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Port 443

这里还有个坑,就是建立的这个config文件不能太公开,否则git不答应,使用

chmod go-rw ~/.ssh/config

这句话就是把g(group当前组)和o(other其他用户)的对config这个文件的权限去掉读和写。

两个有用的文档

  1. git里面用命令行更改远程仓库的URL/使用的协议
  2. 用https的端口(443)代替被封掉的ssh端口(22)

后续

修改完这些。。我以为一切都结束了,可视连接远程仓库还是连接不上,这时候我发现在我项目目录下的 .git/config 里面,远程仓库的地址写的是git://github.com/xxx.git,并不是ssh里面标明的 git@github.com/xx.git 的形式。 那么问题来了,git://git@有什么区别? 进过一番查询,找到了一个解释的比较清楚的答案

It depends on the repository. The native git transport uses TCP port 9418. However, git can also run over ssh (often used for pushing), http, https, and less often others.

You can look at the repository URL to find out which port it uses. Notice that many public repositories have several alternate URLs; for instance, the kernel.org repositories have git://, http://, and https:// URLs.

The common URL schemes for git repositories are:

  • ssh:// - default port 22
  • git:// - default port 9418
  • http:// - default port 80
  • https:// - default port 443

If the URL does not have a scheme, it it using ssh with a slightly different syntax.

See the git fetch manpage for more details on the available URL schemes.

大体意思就是git://使用的是 native git协议,用的是9418端口,有些防火墙会封掉,而git@也就是ssh:用的是ssh协议,端口是22,也会被一些防火墙封掉,上面那些操作只能解决ssh连不上的问题,也就是说执行了上面的配置,只能保证git@github.com这样的远程仓库能连接,而写成git://就无法解决,解决这个,只能利用git的全局配置把所有的git://连接转成https://连接,这样就避免了使用native git协议。 这篇文章对上面的问题说的挺好。

Comments